﻿<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Gasco.Business.Services" %>
<%@ Import Namespace="Gasco.DataAccess" %>
<%@ Import Namespace="Mani.Common" %>

<script runat="server">
    [System.Web.Services.WebMethod]
    public static string FindCities(string countryId)
    {
        string getCityList = "";
        CityService service = new CityService();
        List<city> listcity = (List<city>)service.GetCitiesByCountry(Convert.ToInt32(countryId));
        foreach (city cl in listcity)
        {
            getCityList += cl.id + ";" + cl.name + ",";
        }
        return getCityList;
    }

    [System.Web.Services.WebMethod]
    public static string FindOrderNumberByName(string orderName)
    {
        OrderService orderService = new OrderService();
        return orderService.GetOrderByName(orderName).order_number.ToString();
    }

    [System.Web.Services.WebMethod]
    public static string FindUnitByOrderNumber(string orderNum)
    {
       OrderService orderService=new OrderService();
       return orderService.GetOrderByNumber(orderNum).unit.name.ToString();
    }

    [System.Web.Services.WebMethod]
    public static string FindOrderNameByNumber(string orderNum)
    {
        OrderService orderService = new OrderService();
        return orderService.GetOrderByNumber(orderNum).order_name.ToString();
    }
	
	[System.Web.Services.WebMethod]
    public static string GetPlatts(string productId, string plattDate, string plattTypeId)
    {
        if (plattTypeId == "-1" || productId == "-1")
            return "0";
        
        PlattService ps = new PlattService();

        platt p = ps.GetByProductAndDate(Convert.ToInt32(productId), Convert.ToDateTime(plattDate), Convert.ToInt32(plattTypeId));
        if (p == null)
            return "0";
        
        return UIHelper.FormatString( p.price );
    }

    [System.Web.Services.WebMethod]
    public static string CalculatePrice(string platt, string formula, string quantity)
    {
        if (String.IsNullOrEmpty(platt))
            return "0";
        
        float plattPrice = Convert.ToSingle(UIHelper.RemoveFormat(platt));
        float formulaResult = plattPrice;

        if (!String.IsNullOrEmpty(formula))
        {
            try
            {
                Ceme.CemeParser parser = new Ceme.CemeParser(plattPrice.ToString() + formula);
                formulaResult = Convert.ToSingle(parser.Evaluate());
            }
            catch ( FormatException )
            {
                // do nothing
            }
        }
        
        if ( !String.IsNullOrEmpty(quantity) )
            formulaResult = Convert.ToSingle(quantity) * formulaResult;

        return UIHelper.FormatString(formulaResult);
    }

    [System.Web.Services.WebMethod]
    public static string FindCurrencyByPlatts(string productId, string plattDate, string plattTypeId)
    {
        if (plattTypeId == "-1" || productId == "-1")
            return "";
        
        PlattService ps = new PlattService();

        platt p = ps.GetByProductAndDate(Convert.ToInt32(productId), Convert.ToDateTime(plattDate), Convert.ToInt32(plattTypeId));
        if (p == null)
            return "";

        LookupService<currency> currencyService = new LookupService<currency>();
        return currencyService.FindById(p.currency_id).name; ;
    }

    [System.Web.Services.WebMethod]
    public static float FindOrderQuantityByOrderNum(string orderNum, string loadingId)
    {
        int id = 0;
        if (!String.IsNullOrEmpty(loadingId))
            id = Convert.ToInt32(loadingId);
        
        LoadingService loadingService = new LoadingService();
        return loadingService.RemainLoadingByOrderNum(orderNum, id).Round(3);
    }

    [System.Web.Services.WebMethod]
    public static string GetAveragePriceByDate(string productId, string plattsTypeId, string startDate, string endDate)
    {
        DateTime start_date = startDate.ToDateTime();
        DateTime end_date = endDate.ToDateTime();

        PlattService ps = new PlattService();
        return ps.GetAveragePriceByDate(Convert.ToInt32(productId), Convert.ToInt32(plattsTypeId), start_date, end_date).ToString();
    }
    
</script>